<template>
  <view>
    <view class="search">
      <my-search @click.native="goSearch()"></my-search>
    </view>
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" :circular="true">
      <swiper-item v-for="(item,index) in swiperList" :key="index">
        <navigator :url="`/subpkg/goods_detail/goods_detail?goods_id=`+item.goods_id" class="swiper-item">
          <image :src="item.image_src"></image>
        </navigator>
      </swiper-item>
    </swiper>
    <!-- 导航栏 -->
    <view class="navLink">
      <view @click="navClickHandler(item)" class="box" v-for="(item,index) in navList" :key="index">
        <image :src="item.image_src"></image>
      </view>
    </view>
    <!-- 主体内容 -->
    <view class="floor" v-for="(item,index) in floorList" :key="index">
      <view class="title">
        <image :src="item.floor_title.image_src" class="img-title"></image>
      </view>
      <view class="text">
        <view class="one">
          <image @click="floorClickHandler(item.product_list[0].navigator_url)" :src="item.product_list[0].image_src"
            mode="widthFix" :style="{width:item.product_list[0].image_width+'rpx'}"></image>
        </view>
        <view class="two">
          <image :src="item2.image_src" mode="widthFix" :style="{width: item2.image_width+'rpx'}" v-if="index2!==0"
            v-for="(item2,index2) in item.product_list" :key="index2" @click="floorClickHandler(item2.navigator_url)">
          </image>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  import tabbarMixins from '@/mixins/tabbar.js'
  export default {
    mixins: [tabbarMixins],
    data() {
      return {
        swiperList: [],
        navList: [],
        floorList: []
      };
    },
    methods: {
      async getSwiperList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/swiperdata')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.swiperList = res.message
      },
      async getNavList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/catitems')
        if (res.meta.status !== 200) {
          return uni.$showMsg()
        }
        this.navList = res.message
      },
      navClickHandler(item) {
        switch (item.name) {
          case "分类":
            uni.switchTab({
              url: "/pages/cate/cate"
            })
            break
          case "秒杀拍":
            console.log(2)
            break
          case "超市购":
            console.log(3)
            break
          case "母婴品":
            console.log(4)
            break
        }
      },
      async getFloorList() {
        const {
          data: res
        } = await uni.$http.get('/api/public/v1/home/floordata')
        if (res.meta.status !== 200) {
          uni.$showMsg()
        }
        res.message.forEach(item1 => {
          item1.product_list.forEach(item2 => {
            item2.navigator_url = "/subpkg/goods-list/goods-list?" + item2.navigator_url.split('?')[1]
          })
        })
        this.floorList = res.message
      },
      floorClickHandler(url) {
        uni.navigateTo({
          url
        })
      },
      goSearch() {
        uni.navigateTo({
          url: "/subpkg/goods-search/goods-search"
        })
      }
    },
    onLoad() {
      this.getSwiperList()
      this.getNavList()
      this.getFloorList()
    }
  }
</script>

<style lang="scss">
  .search {
    position: sticky;
    top: 0;
    z-index: 999;
  }

  swiper {
    height: 330rpx;

    .swiper-item {
      width: 100%;

      image {
        width: 100%;
      }
    }
  }

  .navLink {
    display: flex;
    justify-content: space-around;
    margin-top: 20rpx;

    .box {

      image {
        width: 128rpx;
        height: 140rpx;
      }
    }
  }

  .floor {
    .title {
      margin-top: 20rpx;

      .img-title {
        width: 100%;
        height: 60rpx;
      }
    }

    .text {
      display: flex;

      .one {
        margin-left: 20rpx;
      }

      .two {
        margin-left: 10rpx;

        image {
          margin: 0 5rpx;
        }
      }
    }
  }
</style>